<template>
  <div class="app-container">
    <el-table v-loading="listLoading" :data="list" border fit highlight-current-row style="width: 100%">
      <el-table-column align="center" label="ID" width="80">
        <template slot-scope="scope">
          <span>{{ scope.row.id }}</span>
        </template>
      </el-table-column>

      <el-table-column min-width="100px" label="视频名称">
        <template slot-scope="{row}">
          <span>{{ row.title }}</span>
        </template>
      </el-table-column>

      <el-table-column min-width="400px" label="视频简介">
        <template slot-scope="{row}">
          <span>{{ row.description }}</span>
        </template>
      </el-table-column>

      <el-table-column min-width="100px" label="投票次数">
        <template slot-scope="{row}">
          <span>{{ row.vote }}</span>
        </template>
      </el-table-column>

      <el-table-column align="center" label="操作" width="240">
        <template slot-scope="scope">
          <router-link :to="'/video/edit/'+scope.row.id">
            <el-button type="primary" size="small" icon="el-icon-edit">
              修改
            </el-button>
          </router-link>

          <el-button type="danger" size="small" icon="el-icon-delete" @click="deleteVideo(scope.row.id)">
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total>0" :total="total" :page.sync="listQuery.page" :limit.sync="listQuery.limit" @pagination="getList" />
  </div>
</template>

<script>
import { fetchList, deleteVideo } from '@/api/video'
import Pagination from '@/components/Pagination' // Secondary package based on el-pagination
import { jsError } from '@/api/js-error'

export default {
  name: 'VideoList',
  components: { Pagination },
  data() {
    return {
      list: null,
      total: 0,
      // 加载数据期间显示旋转图标
      listLoading: true,
      listQuery: {
        page: 1,
        limit: 10
      }
    }
  },
  created() {
    this.getList()
  },
  methods: {
    getList() {
      this.listLoading = true
      fetchList(this.listQuery).then(response => {
        this.list = response.data.items
        this.total = response.data.total
        this.listLoading = false
      })
    },
    deleteVideo(id) {
      deleteVideo(id)
        .then(data => {
          if (data.code === 20000) {
            this.$notify({
              title: '成功',
              message: '删除视频成功',
              type: 'success',
              duration: 2000
            })
            this.getList()
          }
        })
        .catch(err => {
          jsError({
            filename: 'video.list.vue',
            title: 'deleteVideo(' + id + ')',
            message: err.message
          })
        })
    }
  }
}
</script>

<style scoped>
.edit-input {
  padding-right: 100px;
}
.cancel-btn {
  position: absolute;
  right: 15px;
  top: 10px;
}
</style>
